package avicit.cbb.techclass.tabledefine.cbbtabcolcodetl.controller.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import avicit.cbb.techclass.tabledefine.cbbtabcolcodetl.dao.CbbTabColCodeTlDao;
import avicit.cbb.techclass.tabledefine.cbbtabcolcodetl.dto.CbbTabColCodeTlDTO;
import avicit.platform6.api.syslanguage.SysLanguageAPI;
import avicit.platform6.api.syslanguage.dto.SysLanguage;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.properties.PlatformConstant.OpType;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.service.ServiceBase;
import avicit.platform6.core.sfn.intercept.SelfDefinedQuery;
import avicit.platform6.core.mybatis.pagehelper.Page;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.mybatis.pagehelper.PageHelper;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;

/**
 * @classname:  CbbTabColCodeTlService
 * @description: 定义 参考模板多语言表实现类
 * @author:  AVICIT DEV
 */
@Service
@SuppressWarnings("unchecked")
public class CbbTabColCodeTlService extends ServiceBase implements Serializable {

	private static final Logger logger = LoggerFactory.getLogger(CbbTabColCodeTlService.class);

	private static final long serialVersionUID = 1L;

	// @Autowired
	// private SysLogUtil sysLogUtil;

	@Autowired
	private CbbTabColCodeTlDao dao;

	@Autowired
	private SysLanguageAPI sysLanguageAPI;

	/**
	 * 按条件分页查询
	 * @param queryReqBean
	 * @return
	 * @throws Exception
	 */
	public QueryRespBean<CbbTabColCodeTlDTO> searchCbbTabColCodeTlByPage(QueryReqBean<CbbTabColCodeTlDTO> queryReqBean, String sfnConditions) throws Exception {
		try {
			PageHelper.startPage(queryReqBean.getPageParameter());
			CbbTabColCodeTlDTO searchParams = queryReqBean.getSearchParams();
			SelfDefinedQuery sdc = new SelfDefinedQuery(sfnConditions, "t1");
			Page<CbbTabColCodeTlDTO> dataList = dao.searchCbbTabColCodeTlByPage(searchParams, sdc.parseSql());
			QueryRespBean<CbbTabColCodeTlDTO> queryRespBean = new QueryRespBean<CbbTabColCodeTlDTO>();

			queryRespBean.setResult(dataList);
			return queryRespBean;
		} catch (DaoException e) {
			logger.error("searchCbbTabColCodeTlByPaging出错：", e);
			throw e;
		}
	}

	/**
	 * 按条件查询，不分页
	 * @param queryReqBean
	 * @return
	 * @throws Exception
	 */
	public List<CbbTabColCodeTlDTO> searchCbbTabColCodeTl(QueryReqBean<CbbTabColCodeTlDTO> queryReqBean) throws Exception {
		try {
			CbbTabColCodeTlDTO searchParams = queryReqBean.getSearchParams();
			List<CbbTabColCodeTlDTO> dataList = dao.searchCbbTabColCodeTl(searchParams);
			return dataList;
		} catch (Exception e) {
			logger.error("searchCbbTabColCodeTl出错：", e);
			throw new DaoException("", e);
		}
	}

	/**
	 * 通过主键查询单条记录
	 * @param id
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public CbbTabColCodeTlDTO queryCbbTabColCodeTlByPrimaryKey(String id) throws Exception {
		try {
			CbbTabColCodeTlDTO dto = dao.findCbbTabColCodeTlById(id);
			// 记录日志
			SysLogUtil.log4Query(dto);
			return dto;
		} catch (Exception e) {
			logger.error("searchCbbTabColCodeTl出错：", e);
			throw new DaoException("", e);
		}
	}

	/**
	 * 新增对象
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public String insertCbbTabColCodeTl(CbbTabColCodeTlDTO dto) throws Exception {
		try {
			String id = ComUtil.getId();
			dto.setId(id);
			PojoUtil.setSysProperties(dto, OpType.insert);
			dao.insertCbbTabColCodeTl(dto);
			// 记录日志
			SysLogUtil.log4Insert(dto);
			return id;
		} catch (Exception e) {
			logger.error("insertCbbTabColCodeTl出错：", e);
			throw new DaoException("", e);
		}
	}

	/**
	 * 修改对象全部字段
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int updateCbbTabColCodeTl(CbbTabColCodeTlDTO dto) throws Exception {
		// 记录日志
		CbbTabColCodeTlDTO old = findById(dto.getId());
		SysLogUtil.log4Update(dto, old);
		PojoUtil.setSysProperties(dto, OpType.update);
		PojoUtil.copyProperties(old, dto, true);
		int ret = dao.updateCbbTabColCodeTlAll(old);
		if (ret == 0) {
			throw new DaoException("数据失效，请从新更新");
		}
		return ret;

	}

	/**
	 * 修改对象部分字段
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int updateCbbTabColCodeTlSensitive(CbbTabColCodeTlDTO dto) throws Exception {
		try {
			// 记录日志
			CbbTabColCodeTlDTO old = findById(dto.getId());
			SysLogUtil.log4Update(dto, old);
			PojoUtil.setSysProperties(dto, OpType.update);
			PojoUtil.copyProperties(old, dto, true);
			int count = dao.updateCbbTabColCodeTlSensitive(old);
			if (count == 0) {
				throw new DaoException("数据失效，请从新更新");
			}
			return count;
		} catch (Exception e) {
			logger.error("searchDemoBusinessTrip出错：", e);
			throw new DaoException("", e);
		}

	}

	/**
	 * 按主键单条删除
	 * @param id
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int deleteCbbTabColCodeTlById(String id) throws Exception {
		if (StringUtils.isEmpty(id))
			throw new Exception("删除失败！传入的参数主键为null");
		try {
			// 记录日志
			SysLogUtil.log4Delete(findById(id));
			return dao.deleteCbbTabColCodeTlById(id);
		} catch (Exception e) {
			logger.error("deleteCbbTabColCodeTl出错：", e);
			throw new DaoException("", e);
		}
	}

	/**
	 * 批量删除数据
	 * @param ids id的数组
	 * @return
	 * @throws Exception
	 */
	public int deleteCbbTabColCodeTlByIds(String[] ids) throws Exception {
		int result = 0;
		for (String id : ids) {
			deleteCbbTabColCodeTlById(id);
			result++;
		}
		return result;
	}

	/**
	 * 日志专用，内部方法，不再记录日志
	 */
	private CbbTabColCodeTlDTO findById(String id) throws Exception {
		try {
			CbbTabColCodeTlDTO dto = dao.findCbbTabColCodeTlById(id);
			return dto;
		} catch (DaoException e) {
			logger.error("findCbbTabColCodeTl出错：", e);
			throw e;
		}
	}

	/**
	 * 获取多语言信息	
	 * @param id
	 * @return
	 */
	public List<Map<String, Object>> getAllLanguageById(String id) throws Exception {
		List<SysLanguage> sysLanguageCollection = this.sysLanguageAPI.getAllSysLanguages();

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = null;
		for (SysLanguage language : sysLanguageCollection) {
			CbbTabColCodeTlDTO temp = getSysLookupTypeTlByLanguageCode(id, language.getLanguageCode());

			map = new HashMap<String, Object>();
			String tlid = "";
			String columnsShowName = "";
			String description = "";
			if (temp != null) {
				tlid = temp.getId();
				columnsShowName = temp.getColumnsShowName();
				description = temp.getDescription();
			}

			map.put("id", tlid);
			map.put("columnsShowName", columnsShowName);
			map.put("description", description);
			map.put("sysLanguageCode", language.getLanguageCode());
			map.put("sysLanguageName", language.getLanguageName());
			map.put("cbbTabColCodeId", id);

			result.add(map);
		}
		return result;
	}

	@SuppressWarnings({"rawtypes", "static-access"})
	public CbbTabColCodeTlDTO getSysLookupTypeTlByLanguageCode(String pId, String sysLanguageCode) {
		if (null == pId)
			return null;
		Assert.notNull(sysLanguageCode);
		String sql = "select  * from CBB_TAB_COL_CODE_TL t where t.CBB_TAB_COL_CODE_ID =:pid and t.sys_language_code =:language";
		MapSqlParameterSource ss = new MapSqlParameterSource();
		ss.addValue("pid", pId, 12);
		ss.addValue("language", sysLanguageCode, 12);
		try {
			return (CbbTabColCodeTlDTO) this.jdbc.getNamedParameterJdbcOperations().queryForObject(sql, ss, new BeanPropertyRowMapper(CbbTabColCodeTlDTO.class));
		} catch (EmptyResultDataAccessException e) {
			this.logger.debug("没有查到数据！");
		}
		return null;
	}

	@Override
	public void delete(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub

	}

	@Override
	public void insert(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub

	}
}
